package com.miracle9.game.thread;

import com.miracle9.game.service.CenterBIService;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.annotation.PostConstruct;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("centerBIThread")
public class CenterBIThread
  extends Thread
{
  private Logger logger = Logger.getLogger(CenterBIThread.class);
  @Autowired
  private CenterBIService Q;
  private final String R = "send.data";
  private String S = "true";
  
  private String readerSendFile()
  {
    BufferedReader reader = null;
    String data = null;
    try
    {
      reader = new BufferedReader(new InputStreamReader(new FileInputStream("send.data"), "UTF-8"));
      data = reader.readLine();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      this.logger.error("readData eror:" + e);
      if (reader != null) {
        try
        {
          reader.close();
        }
        catch (IOException e1)
        {
          e1.printStackTrace();
        }
      }
      if (reader != null) {
        try
        {
          reader.close();
        }
        catch (IOException e1)
        {
          e1.printStackTrace();
        }
      }
    }
    finally
    {
      if (reader != null) {
        try
        {
          reader.close();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }
    }
    return data;
  }
  
  private void writeSendFile(String data)
  {
    BufferedWriter fw = null;
    try
    {
      fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("send.data"), "UTF-8"));
      fw.write(data);
      fw.flush();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      this.logger.error("writeData error:" + e);
      if (fw != null) {
        try
        {
          fw.close();
        }
        catch (IOException e1)
        {
          e1.printStackTrace();
        }
      }
      if (fw != null) {
        try
        {
          fw.close();
        }
        catch (IOException e1)
        {
          e1.printStackTrace();
        }
      }
    }
    finally
    {
      if (fw != null) {
        try
        {
          fw.close();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }
    }
  }
  
  @PostConstruct
  public void voidMaethod() {}
  
  public void run()
  {
    for (;;)
    {
      String data = readerSendFile();
      if (data != null) {
        this.S = data;
      }
      try
      {
        this.logger.info("can send:" + this.S);
        if ("true".equals(this.S))
        {
          this.Q.sendSaleSet();
          this.Q.sendRechargeGains();
          this.Q.sendRunStatitistics();
          this.Q.sendSubordinateNumber();
          this.Q.sendUserOnLineDuration();
        }
        writeSendFile("false");
        sleep(86400000L);
        writeSendFile("true");
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
  }
}
